package com.aos.erp.schools.rest.car;


import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.model.car.Car;
import com.aos.erp.schools.model.car.CarVo.CarVo;
import com.aos.erp.schools.model.car.Dto.CarDto;
import com.aos.erp.schools.service.car.CarService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Slf4j
@RestController("CarController")
@RequestMapping("/api/car")
@Api(value = "车辆接口", tags = {"车辆接口"})
public class CarController {
    @Resource
    private CarService carService;

    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = " 每页显示条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "carNumbers", value = "车牌号码", required = false, dataType = "String", paramType = "Query"),
            // @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "Query"),
            // @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "Query"
            @ApiImplicitParam(name = "registerTime", value = "结束时间", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/list")
    public ResultEntity<IPage<CarVo>> queryPageBean(@RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(name = "carNumbers", required = false) String carNumbers,
                                                    //@RequestParam(name = "startTime", required = false) String startTime,
                                                    // @RequestParam(name = "endTime", required = false) String endTime,
                                                    @RequestParam(name = "registerTime", required = false) String registerTime,
                                                    @RequestParam(name = "currentUserId", required = false) String currentUserId) throws Exception {

        //TODO 驾校部门组织code
        String schoolDepCode = UserUtil.getUserDepCode(currentUserId);
        if (StringUtils.isBlank(schoolDepCode)) {
            return ResultEntity.failed("驾校部门组织code不能为空", ResultEntity.CODE_NULL);
        }

        return ResultEntity.success(carService.queryByPage(pageIndex, pageSize, carNumbers, registerTime, schoolDepCode));
    }

    @PostMapping("/addCar")
    @ApiOperation(value = "新增车辆")
    public ResultEntity addCar(@Validated CarDto carDto,  BindingResult bindingResult,String currentUserId) throws Exception {
        if (StringUtils.isEmpty(currentUserId)) {
            return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
        }
        //数据 验证信息
        if (bindingResult.hasErrors()) {
            return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        //TODO 驾校id
        String driversSchoolId = UserUtil.getUserSchoolId(currentUserId);
        if(driversSchoolId==null){
            return ResultEntity.failed("驾校id数据不存在");
        }
        carDto.setDriversSchoolId(driversSchoolId);
        carService.addBean(carDto);
        return ResultEntity.success();
    }

    @ApiOperation(value = "删除车辆", notes = "删除车辆")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultEntity deleteClass(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("车辆ID不能为空", ResultEntity.CODE_NULL);
        }
        return carService.isDel(id) >= 1 ? ResultEntity.success() : ResultEntity.failed();
    }

    @ApiOperation(value = "获取车辆信息", notes = "获取车辆信息")
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public ResultEntity getInfo(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("车辆ID不能为空", ResultEntity.CODE_NULL);
        }
        CarVo carVo = carService.getInfo(id);
        return ResultEntity.success(carVo);
    }

    @PostMapping("/updateCar")
    @ApiOperation(value = "修改车辆")
    public ResultEntity updateCar(@Validated CarDto carDto, String currentUserId) throws Exception {
        if (StringUtils.isEmpty(currentUserId)) {
            return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
        }
        //TODO 驾校id
        String driversSchoolId = UserUtil.getUserSchoolId(currentUserId);
        carDto.setDriversSchoolId(driversSchoolId);
        carService.updateObject(carDto);
        return ResultEntity.success();
    }

}